package com.sinodata.bsm.cicp.collector.weblogic8;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:04:36   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class JmxConnectionCollector extends Collector {

    private static final Logger logger = Logger.getLogger(JmxConnectionCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String port = this.getParamMap().get("Port").trim();//端口
        String username = this.getParamMap().get("Username").trim();//用户名
        String password = this.getParamMap().get("Password").trim();//密码
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        String xmlString = XmlGetter.getJmxXml("WlgJmxConnectionInfo", ip, port, username, password);
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        if (xmlString == null) {
            propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
            //   values.add(new String[] { "NO" });
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        }
        // parse xml
        Document document;
        try {
            document = DocumentHelper.parseText(xmlString);
            Element rootElement = document.getRootElement();
            Element element = rootElement.element("JmxConnectionInfo");
            if (element != null) {
                //                String canConnect = element.element("canConnect").getTextTrim();
                propValues[0] = ResPropertyValue.createPropValue("canConnect", propMetas[0]);
                //values.add(new String[] { canConnect });
            } else {
                propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
                //values.add(new String[] { "NO" });
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (DocumentException e) {
            logger.error("failed to collect by JmxConnectionCollector", e);
            propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
            //values.add(new String[] { "NO" });
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        }
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "用户名", "密码" };
        return new String[] { "Port", "Username", "Password" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "是否可以连接" };
        return new String[] { "WhetherCanConnect" };
    }
}
